package com.xiaobai.zhuangxiuguanjia.activities;

import android.content.Intent;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.animation.AnimationUtils;
import android.view.animation.LayoutAnimationController;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.JsonObjectRequest;
import com.xiaobai.zhuangxiuguanjia.R;
import com.xiaobai.zhuangxiuguanjia.Utils.RequestManager;
import com.xiaobai.zhuangxiuguanjia.Utils.ViewHolder;
import com.xiaobai.zhuangxiuguanjia.adapters.FancyCoverFlowSampleAdapter;
import com.xiaobai.zhuangxiuguanjia.model.ReadData;
import com.xiaobai.zhuangxiuguanjia.model.ReadRespense;
import com.xiaobai.zhuangxiuguanjia.model.ValueData;

import org.json.JSONObject;

import java.util.LinkedList;
import java.util.List;

import at.technikum.mti.fancycoverflow.FancyCoverFlow;

public class SimpleActivity extends FragmentActivity
        implements AdapterView.OnItemSelectedListener, View.OnClickListener,
        AdapterView.OnItemClickListener {

    private FancyCoverFlow fancyCoverFlow;
    private ListView listView;
    private List<ReadData> readDataList;
    private List<ValueData> valueDataList;
    private MyAdapter myAdapter;
    private int selection;
    private TextView textView;
    private TextView txtSign;
    private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_simple);
        listView = (ListView) findViewById(R.id.read_listView);
        textView = (TextView) findViewById(R.id.read_txt_title);
        txtSign = (TextView) findViewById(R.id.read_txt_sign);
        button = (Button) findViewById(R.id.btn_back);
        button.setOnClickListener(this);
        readDataList = new LinkedList<ReadData>();
        valueDataList = new LinkedList<ValueData>();
        this.fancyCoverFlow = (FancyCoverFlow) this.findViewById(R.id.fancyCoverFlow);
        FancyCoverFlowSampleAdapter adapter = new FancyCoverFlowSampleAdapter();
        this.fancyCoverFlow.setAdapter(adapter);
        this.fancyCoverFlow.setUnselectedAlpha(0.3f);
        this.fancyCoverFlow.setUnselectedSaturation(0.0f);
        this.fancyCoverFlow.setUnselectedScale(0.5f);
        this.fancyCoverFlow.setSpacing(1);
        this.fancyCoverFlow.setMaxRotation(0);
        this.fancyCoverFlow.setScaleDownGravity(0.1f);
        fancyCoverFlow.setY(80);
        this.fancyCoverFlow.setActionDistance(FancyCoverFlow.ACTION_DISTANCE_AUTO);
        fancyCoverFlow.setOnItemSelectedListener(this);
        listView.setOnItemClickListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        RequestQueue requestQueue = RequestManager.getInstance(this).getRequestQueue();
        String url = "http://mobileapi.to8to.com/smallapp.php?uid=&model=decorationKnowle&action=list&appid=1&appversion=2.5.1&systemversion=15&appostype=1&to8to_token=&version=2.5&";
        JsonObjectRequest request = new JsonObjectRequest(
                url,
                null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        ReadRespense readRespense = new ReadRespense();
                        readRespense.parseJSON(response);
                        List<ReadData> readDatas = readRespense.getReadDataList();
                        readDataList.addAll(0, readDatas);
                        valueDataList = readDataList.get(0).getValueDataList();
                        textView.setText(readDataList.get(selection).getName());
                        txtSign.setText(readDataList.get(selection).getName() + "阶段必读");
                        myAdapter = new MyAdapter(valueDataList);
                        listView.setAdapter(myAdapter);
                        myAdapter.notifyDataSetChanged();
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                }
        );
        requestQueue.add(request);

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return super.onTouchEvent(event);

    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        selection = position;
        if (!readDataList.isEmpty()) {
            List<ValueData> valueDatas = readDataList.get(selection).getValueDataList();
            textView.setText(readDataList.get(selection).getName());
            txtSign.setText(readDataList.get(selection).getName() + "阶段必读");
            myAdapter = new MyAdapter(valueDatas);
            listView.setAdapter(myAdapter);
            myAdapter.notifyDataSetChanged();
            LayoutAnimationController controller =
                    new LayoutAnimationController(AnimationUtils.loadAnimation(this, R.anim.list_zoom_in));
            controller.setOrder(LayoutAnimationController.ORDER_NORMAL);
            listView.setLayoutAnimation(controller);
            listView.startLayoutAnimation();
        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }

    @Override
    public void onClick(View v) {
        finish();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        List<ValueData> valueDataList = readDataList.get(selection).getValueDataList();
        String kid = valueDataList.get(position).getKid();
        String url = "http://mobileapi.to8to.com/smallapp.php?module=Decoration&action=Knowledetail_V2_1&version=2.5&android=1&kid=" + kid + "&appversion=2.5.1&systemversion=15&to8to_token=null&appostype=1";
        Intent intent = new Intent();
        intent.putExtra("url", url);
        intent.putExtra("title", valueDataList.get(position).getTitle());
        intent.setClass(this, WebActivity.class);
        startActivity(intent);
    }

    private class MyAdapter extends BaseAdapter {
        private List<ValueData> list;

        public MyAdapter(List<ValueData> list) {
            this.list = list;
        }

        @Override
        public int getCount() {
            int ret = 0;
            if (list != null) {
                ret = list.size();
            }
            return ret;
        }

        @Override
        public Object getItem(int position) {
            Object ret = null;
            if (list != null) {
                list.get(position);
            }
            return ret;
        }

        @Override
        public long getItemId(int position) {
            long ret = 0;
            if (list != null) {
                ret = position;
            }
            return ret;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            if (convertView == null) {
                convertView = LayoutInflater.from(SimpleActivity.this).inflate(R.layout.read_item_layout, parent, false);
            }
            ImageView imageView = ViewHolder.getView(convertView, R.id.read_item_image);
            TextView txtTitle = ViewHolder.getView(convertView, R.id.read_item_title);
            TextView txtContent = ViewHolder.getView(convertView, R.id.read_item_content);
            String imgurl = list.get(position).getImgurl();
            ImageLoader imageLoader = RequestManager.getInstance(SimpleActivity.this).getImageLoader();
            imageLoader.get(
                    imgurl,
                    imageLoader.getImageListener(imageView, R.mipmap.ic_launcher, R.mipmap.ic_launcher)
            );
            String title = list.get(position).getTitle();
            txtTitle.setText(title);
            String page_description = list.get(position).getPage_description();
            txtContent.setText(page_description);
            return convertView;

        }
    }
}
